home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / abc.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  6KB  |  163 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Abstract Base Classes (ABCs) according to PEP 3119.'''
  5.  
  6. def abstractmethod(funcobj):
  7.     """A decorator indicating abstract methods.
  8.  
  9.     Requires that the metaclass is ABCMeta or derived from it.  A
  10.     class that has a metaclass derived from ABCMeta cannot be
  11.     instantiated unless all of its abstract methods are overridden.
  12.     The abstract methods can be called using any of the normal
  13.     'super' call mechanisms.
  14.  
  15.     Usage:
  16.  
  17.         class C:
  18.             __metaclass__ = ABCMeta
  19.             @abstractmethod
  20.             def my_abstract_method(self, ...):
  21.                 ...
  22.     """
  23.     funcobj.__isabstractmethod__ = True
  24.     return funcobj
  25.  
  26.  
  27. class abstractproperty(property):
  28.     """A decorator indicating abstract properties.
  29.  
  30.     Requires that the metaclass is ABCMeta or derived from it.  A
  31.     class that has a metaclass derived from ABCMeta cannot be
  32.     instantiated unless all of its abstract properties are overridden.
  33.     The abstract properties can be called using any of the normal
  34.     'super' call mechanisms.
  35.  
  36.     Usage:
  37.  
  38.         class C:
  39.             __metaclass__ = ABCMeta
  40.             @abstractproperty
  41.             def my_abstract_property(self):
  42.                 ...
  43.  
  44.     This defines a read-only property; you can also define a read-write
  45.     abstract property using the 'long' form of property declaration:
  46.  
  47.         class C:
  48.             __metaclass__ = ABCMeta
  49.             def getx(self): ...
  50.             def setx(self, value): ...
  51.             x = abstractproperty(getx, setx)
  52.     """
  53.     __isabstractmethod__ = True
  54.  
  55.  
  56. class ABCMeta(type):
  57.     """Metaclass for defining Abstract Base Classes (ABCs).
  58.  
  59.     Use this metaclass to create an ABC.  An ABC can be subclassed
  60.     directly, and then acts as a mix-in class.  You can also register
  61.     unrelated concrete classes (even built-in classes) and unrelated
  62.     ABCs as 'virtual subclasses' -- these and their descendants will
  63.     be considered subclasses of the registering ABC by the built-in
  64.     issubclass() function, but the registering ABC won't show up in
  65.     their MRO (Method Resolution Order) nor will method
  66.     implementations defined by the registering ABC be callable (not
  67.     even via super()).
  68.  
  69.     """
  70.     _abc_invalidation_counter = 0
  71.     
  72.     def __new__(mcls, name, bases, namespace):
  73.         cls = super(ABCMeta, mcls).__new__(mcls, name, bases, namespace)
  74.         abstracts = set((lambda .0: for name, value in .0:
  75. if getattr(value, '__isabstractmethod__', False):
  76. namecontinue)(namespace.items()))
  77.         for base in bases:
  78.             for name in getattr(base, '__abstractmethods__', set()):
  79.                 value = getattr(cls, name, None)
  80.                 if getattr(value, '__isabstractmethod__', False):
  81.                     abstracts.add(name)
  82.                     continue
  83.             
  84.         
  85.         cls.__abstractmethods__ = frozenset(abstracts)
  86.         cls._abc_registry = set()
  87.         cls._abc_cache = set()
  88.         cls._abc_negative_cache = set()
  89.         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
  90.         return cls
  91.  
  92.     
  93.     def register(cls, subclass):
  94.         '''Register a virtual subclass of an ABC.'''
  95.         if not isinstance(cls, type):
  96.             raise TypeError('Can only register classes')
  97.         isinstance(cls, type)
  98.         if issubclass(subclass, cls):
  99.             return None
  100.         if issubclass(cls, subclass):
  101.             raise RuntimeError('Refusing to create an inheritance cycle')
  102.         issubclass(cls, subclass)
  103.         cls._abc_registry.add(subclass)
  104.         ABCMeta._abc_invalidation_counter += 1
  105.  
  106.     
  107.     def _dump_registry(cls, file = None):
  108.         '''Debug helper to print the ABC registry.'''
  109.         print >>file, 'Class: %s.%s' % (cls.__module__, cls.__name__)
  110.         print >>file, 'Inv.counter: %s' % ABCMeta._abc_invalidation_counter
  111.         for name in sorted(cls.__dict__.keys()):
  112.             if name.startswith('_abc_'):
  113.                 value = getattr(cls, name)
  114.                 print >>file, '%s: %r' % (name, value)
  115.                 continue
  116.         
  117.  
  118.     
  119.     def __instancecheck__(cls, instance):
  120.         '''Override for isinstance(instance, cls).'''
  121.         subclass = getattr(instance, '__class__', None)
  122.         if subclass in cls._abc_cache:
  123.             return True
  124.         subtype = type(instance)
  125.         return subclass is None if subtype is subclass or subclass is None else cls.__subclasscheck__(subtype)
  126.  
  127.     
  128.     def __subclasscheck__(cls, subclass):
  129.         '''Override for issubclass(subclass, cls).'''
  130.         if subclass in cls._abc_cache:
  131.             return True
  132.         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
  133.             cls._abc_negative_cache = set()
  134.             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
  135.         elif subclass in cls._abc_negative_cache:
  136.             return False
  137.         ok = cls.__subclasshook__(subclass)
  138.         if ok is not NotImplemented:
  139.             if not isinstance(ok, bool):
  140.                 raise AssertionError
  141.             if ok:
  142.                 cls._abc_cache.add(subclass)
  143.             else:
  144.                 cls._abc_negative_cache.add(subclass)
  145.             return ok
  146.         if cls in getattr(subclass, '__mro__', ()):
  147.             cls._abc_cache.add(subclass)
  148.             return True
  149.         for rcls in cls._abc_registry:
  150.             if issubclass(subclass, rcls):
  151.                 cls._abc_cache.add(subclass)
  152.                 return True
  153.         
  154.         for scls in cls.__subclasses__():
  155.             if issubclass(subclass, scls):
  156.                 cls._abc_cache.add(subclass)
  157.                 return True
  158.         
  159.         cls._abc_negative_cache.add(subclass)
  160.         return False
  161.  
  162.  
  163.